package ruifight.member.service.impl;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;

import ruifight.member.dao.AccountRecordMapper;
import ruifight.member.dao.ShopAccountMapper;
import ruifight.member.dao.TransactionRecordMapper;
import ruifight.member.model.AccountRecord;
import ruifight.member.model.ShopAccount;
import ruifight.member.model.ShopInfo;
import ruifight.member.model.TransactionRecord;
import ruifight.member.model.TransactionRecordExample;
import ruifight.member.service.TransactionRecordService;
import ruifight.member.service.base.BaseServiceImpl;

@Service
public class TransactionRecordServiceImpl extends BaseServiceImpl<TransactionRecord, TransactionRecordExample, Long> implements TransactionRecordService {

    @Resource
    private TransactionRecordMapper transactionRecordMapper;
    @Resource
    private ShopAccountMapper shopAccountMapper;
    @Resource
    private AccountRecordMapper accountRecordMapper;

    public PageInfo<Map<String, Object>> getTransactionRecordList(int cur_page, int page_size, Map<String, Object> paramMap){
        PageHelper.startPage(cur_page, page_size);
        List<Map<String, Object>> pageList = transactionRecordMapper.getTransactionRecordList(paramMap);
        PageInfo<Map<String, Object>> pageInfo = new PageInfo<Map<String, Object>>(pageList);
        return pageInfo;
    }

    @Transactional(rollbackFor=Exception.class)
	@Override
	public void submitTransaction(TransactionRecord transactionRecord,ShopAccount shopAccount,ShopInfo shopInfo) {
    	int transactionType=transactionRecord.getTransactionType();
    	if(transactionType==1) {
    		ShopAccount sa = shopAccountMapper.selectByPrimaryKey(shopAccount.getShopAccountId());
    		BigDecimal giveMoney =new BigDecimal(transactionRecord.getAttr2());  
    		BigDecimal actualMoney = new BigDecimal(transactionRecord.getMoney());  
    		BigDecimal giveMoneyAndActualMoney = giveMoney.add(actualMoney);  
    		BigDecimal aiscount = giveMoney.divide(giveMoneyAndActualMoney,2,RoundingMode.HALF_UP);
    		sa.setDiscount(aiscount.floatValue());
    		if(StringUtils.isEmpty(sa.getGiveMoney())) {
    			sa.setGiveMoney(giveMoney.add(BigDecimal.valueOf(Long.parseLong("0"))));
    		}else {
    			sa.setGiveMoney(giveMoney.add(sa.getGiveMoney()));
    		}
    		if(StringUtils.isEmpty(sa.getActualMoney())) {
    			sa.setActualMoney(actualMoney.add(BigDecimal.valueOf(Long.parseLong("0"))));
    		}else {
    			sa.setActualMoney(actualMoney.add(sa.getActualMoney()));
    		}
    		if(StringUtils.isEmpty(sa.getShopAccountMoney())) {
    			sa.setShopAccountMoney(giveMoneyAndActualMoney.add(BigDecimal.valueOf(Long.parseLong("0"))));
    		}else {
    			sa.setShopAccountMoney(giveMoneyAndActualMoney.add(sa.getShopAccountMoney()));
    		}
    		shopAccountMapper.updateByPrimaryKeySelective(sa);
    		AccountRecord ar = new AccountRecord();
        	ar.setDraweeName("平台支付");
        	ar.setPayeeId(shopInfo.getUserInfoId());
        	ar.setPayeeShopId(transactionRecord.getShopInfoId());
        	ar.setPayeeShopName(transactionRecord.getShopName());
        	ar.setPayable(giveMoneyAndActualMoney.toString());
        	ar.setPayment(giveMoneyAndActualMoney.toString());
        	ar.setAttr3(sa.getShopAccountMoney()+"");
        	ar.setAccountRecordState(2);
        	ar.setCreateDate(new Date());
    		accountRecordMapper.insertSelective(ar );
    	}else {
    		ShopAccount sa = shopAccountMapper.selectByPrimaryKey(shopAccount.getShopAccountId());
    		BigDecimal actualMoney = new BigDecimal(transactionRecord.getMoney());  
    		BigDecimal discount = new BigDecimal(sa.getDiscount());  
    		if(StringUtils.isEmpty(sa.getActualMoney())) {
    			sa.setActualMoney(actualMoney.add(BigDecimal.valueOf(Long.parseLong("0"))));
    		}else {
    			sa.setActualMoney(actualMoney.add(sa.getActualMoney()));
    		}
    		if(StringUtils.isEmpty(sa.getShopAccountMoney())) {
    			sa.setShopAccountMoney(actualMoney);
    		}else {
    			BigDecimal actualPayment = discount.divide(actualMoney,0,BigDecimal.ROUND_HALF_UP);
    			sa.setShopAccountMoney(actualMoney.add(actualPayment));
    		}
    		shopAccountMapper.updateByPrimaryKeySelective(sa);
    		AccountRecord ar = new AccountRecord();
    		ar.setDraweeName("平台支付");
    		ar.setPayeeId(shopInfo.getUserInfoId());
        	ar.setPayeeShopId(transactionRecord.getShopInfoId());
        	ar.setPayeeShopName(transactionRecord.getShopName());
        	ar.setPayable(actualMoney.toString());
        	ar.setPayment(actualMoney.toString());
        	ar.setAttr3(sa.getShopAccountMoney()+"");
        	ar.setAccountRecordState(2);
        	ar.setCreateDate(new Date());
    		accountRecordMapper.insertSelective(ar );
    	}
    	transactionRecord.setAttr1("1");
    	
    	transactionRecordMapper.updateByPrimaryKeySelective(transactionRecord);
	}
    
}
